package com.hangxin.word.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hangxin.common.core.controller.BaseController;
import com.hangxin.common.core.domain.AjaxResult;
import com.hangxin.common.core.page.TableDataInfo;
import com.hangxin.common.utils.StringUtils;
import com.hangxin.word.domain.StandardWordInfo;
import com.hangxin.word.domain.VocabMgrWord;
import com.hangxin.word.service.IVocabMgrWordService;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 词库管理 Controller
 *
 * @author ZX
 */
@RestController
@RequestMapping("/word/vocab")
public class VocabMgrWordController extends BaseController {

    @Autowired
    private IVocabMgrWordService vocabMgrWordService;

    /**
     * 查询词库列表
     * type为0查询新词 type为1查询专业词 默认0
     */
    @GetMapping("/list")
    public TableDataInfo list(VocabMgrWord vocabMgrWord) {
        startPage();
        List<VocabMgrWord> standardWordInfos = vocabMgrWordService.list(
                Wrappers.<VocabMgrWord>lambdaQuery()
                        .like(StringUtils.isNotBlank(vocabMgrWord.getWord()), VocabMgrWord::getWord, vocabMgrWord.getWord())
                        .eq(vocabMgrWord.getStatus() != null, VocabMgrWord::getStatus, vocabMgrWord.getStatus())
                        .eq(VocabMgrWord::getType, Optional.ofNullable(vocabMgrWord.getType()).orElse(0))
        );
        return getDataTable(standardWordInfos);
    }

    /**
     * 新增词库记录（统一入口）
     * 前端只需传 word、type；待确认(status=0)入库
     */
    @PostMapping
    public AjaxResult add(@RequestBody VocabMgrWord vocabMgrWord) {
        boolean exists = vocabMgrWordService.lambdaQuery()
                .eq(VocabMgrWord::getWord, vocabMgrWord.getWord())
                .exists();
        if (exists) {
            return AjaxResult.error("该词已存在");
        }

        VocabMgrWord entity = new VocabMgrWord();
        entity.setWord(vocabMgrWord.getWord());
        entity.setType(vocabMgrWord.getType());
        entity.setSource(1);
        entity.setStatus(0);
        entity.setCreatedAt(LocalDateTime.now());

        boolean success = vocabMgrWordService.save(entity);
        return success ? AjaxResult.success(entity.getId())
                : AjaxResult.error("新增失败");
    }

    /**
     * 将新词确认为专业词
     * @param ids 新词ID列表（支持批量）
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody @NotEmpty List<Long> ids) {
        boolean ok = vocabMgrWordService.confirmProfessional(ids);
        return ok ? AjaxResult.success() : AjaxResult.error("操作失败");
    }

    /**
     * 根据 ID 删除单条记录
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        boolean success = vocabMgrWordService.removeById(id);
        return success ? AjaxResult.success() : AjaxResult.error("删除失败");
    }

    /**
     * 批量删除
     * 请求体：["id1","id2",...]
     */
    @DeleteMapping("/batch/{ids}")
    public AjaxResult removeBatch(@PathVariable List<Long> ids) {
        if (ids.isEmpty()) {
            return AjaxResult.error("请选择要删除的数据");
        }
        boolean success = vocabMgrWordService.removeByIds(ids);
        return success ? AjaxResult.success() : AjaxResult.error("删除失败");
    }

}
